package com.feisi.dao;

import com.feisi.dao.sqlProvider.ClueSqlProvider;
import com.feisi.domain.Clue;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClueDao {


    /**
     * 查询所有线索,包括负责人姓名(从user表中获取)
     *
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from clue limit #{page},#{limit}")
    @Results(id = "clueMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "cl_name", property = "cl_name"),
            @Result(column = "cl_source", property = "cl_source"),
            @Result(column = "cl_phone", property = "cl_phone"),
            @Result(column = "cl_e_mail", property = "cl_e_mail"),
            @Result(column = "cl_industry", property = "cl_industry"),
            @Result(column = "cl_level", property = "cl_level"),
            @Result(column = "cl_address", property = "cl_address"),
            @Result(column = "cl_next_time", property = "cl_next_time"),
            @Result(column = "cl_creator", property = "cl_creator"),
            @Result(column = "cl_create_time", property = "cl_create_time"),
            @Result(column = "cl_comment", property = "cl_comment"),
            @Result(column = "cl_userid", property = "user",
                    one = @One(select = "com.feisi.dao.UserDao.findUserById", fetchType = FetchType.EAGER)),})
    List<Clue> findAll(@Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 查询总条数
     *
     * @return
     */
    @Select("select count(*) from clue")
    Integer cluesTotal();

    /**
     * 根据线索名称查询线索
     */
    @Select("select * from clue where cl_name=#{cl_name} limit #{page},#{limit}")
    @ResultMap("clueMap")
    List<Clue> findByName(@Param("cl_name") String cl_name, @Param("page") Integer page, @Param("limit") Integer limit);

    /**
     * 根据线索名称查询总条数
     *
     * @return
     */
    @Select("select count(*) from clue where cl_name=#{cl_name}")
    Integer cluesTotalByName(@Param("cl_name") String cl_name);


    /**
     * 根据用户id查出此用户负责的线索
     * 实现 我负责的线索 模块
     * @param userid
     * @param page
     * @param limit
     * @return
     */
    @Select("select * from clue where cl_userid=#{userid} limit #{page},#{limit}")
    @ResultMap("clueMap")
    List<Clue> findByUserid(@Param("userid") Integer userid, @Param("page") Integer page, @Param("limit") Integer limit);



    /**
     * 根据用户id查出此用户负责的线索
     * 实现 我负责的线索 功能
     * @param userid
     * @return
     */
    @Select("select count(*) from clue where cl_userid=#{userid} ")
    Integer clueTotalByUserid(@Param("userid") Integer userid);


    /**
     * 新增线索
     *
     * @param clue
     */
    @Insert("insert into clue values(#{id},#{cl_name},#{cl_source},#{cl_phone},#{cl_e_mail}," +
            "#{cl_industry},#{cl_level},#{cl_address},#{cl_next_time},#{cl_creator}," +
            "#{cl_create_time},#{cl_comment},#{user.id})")
    void saveClue(Clue clue);


    /**
     * 根据id删除线索
     * 批量删除
     * @param ids
     */
    @DeleteProvider(type = ClueSqlProvider.class, method = "deleteByIds")
    void deleteClue(@Param("ids") Integer[] ids);

    /**
     * 修改线索
     * @param clue
     */
    @Update("update clue set cl_name=#{cl_name},cl_source=#{cl_source},cl_phone=#{cl_phone},cl_e_mail=#{cl_e_mail}," +
            "cl_industry=#{cl_industry},cl_level=#{cl_level},cl_address=#{cl_address},cl_next_time=#{cl_next_time}," +
            "cl_comment=#{cl_comment},cl_userid=#{user.id} where id=#{id}")
    void updateClue(Clue clue);


    /**
     * 通过线索id查询线索
     * @param id
     * @return
     */
    @Select("select * from clue where id=#{id}")
    @ResultMap("clueMap")
    Clue findById(@Param("id") Integer id);

}
